package com.guaju.yueshangLife.activity;

import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.databinding.ActivitySelectRefundTypeBinding;

public class SelectRefundTypeActivity extends Activity {

    private ActivitySelectRefundTypeBinding binding;
    RadioButton[] radioButtons;
    private int selectedType = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_select_refund_type);
        initView();
    }

    private void initView() {
        String s1 = binding.rb1.getText().toString();
        String s2 = binding.rb2.getText().toString();
        String s3 = binding.rb3.getText().toString();


        SpannableString ss1 = new SpannableString(s1);
        ss1.setSpan(new ForegroundColorSpan(Color.parseColor("#222222")), 0, 4, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        SpannableString ss2 = new SpannableString(s2);
        ss2.setSpan(new ForegroundColorSpan(Color.parseColor("#222222")), 0, 3, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        SpannableString ss3 = new SpannableString(s3);
        ss3.setSpan(new ForegroundColorSpan(Color.parseColor("#222222")), 0, 2, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        binding.rb1.setText(ss1);
        binding.rb2.setText(ss2);
        binding.rb3.setText(ss3);
        int offset = getResources().getDimensionPixelOffset(R.dimen.dp_16);
        radioButtons = new RadioButton[]{binding.rb1, binding.rb2, binding.rb3};
        for (int i = 0; i < radioButtons.length; i++) {
            Drawable drawable = getResources().getDrawable(R.drawable.selector_nike);
            Rect r = new Rect(0, 0, offset, offset);
            drawable.setBounds(r);
            radioButtons[i].setCompoundDrawablesRelative(null, null, drawable, null);
        }

        //设置布局填充满宽度
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(layoutParams);

        Intent intent = getIntent();
        int selected = intent.getIntExtra(RefundRequestActivity.REQUEST_TYPE_SELECTED, -1);
        if (selected >= 0 && selected <= 2) {
            switch (selected) {
                case 0:
                    binding.rb1.setChecked(true);
                    break;
                case 1:
                    binding.rb2.setChecked(true);
                    break;
                case 2:
                    binding.rb3.setChecked(true);
                    break;
                default:
                    binding.rb1.setChecked(true);
                    break;
            }

        }
    }

    @Override
    public void finish() {
        String typeString = "";
        int checkedRadioButtonId = binding.radiogroup.getCheckedRadioButtonId();
        switch (checkedRadioButtonId) {
            case R.id.rb1:
                typeString = "退货退款";
                selectedType = 0;
                break;
            case R.id.rb2:
                typeString = "仅退款";
                selectedType = 1;
                break;
            case R.id.rb3:
                typeString = "换货";
                selectedType = 2;
                break;
            default:
                break;

        }
        Intent intent = new Intent();
        intent.putExtra(RefundRequestActivity.REQUEST_TYPE, typeString);
        intent.putExtra(RefundRequestActivity.REQUEST_TYPE_SELECTED, selectedType);
        setResult(RefundRequestActivity.RESULT_SELECT_TYPE, intent);
        super.finish();


    }
}
